package com.xdjd.mobile.system.interceptor;

import com.alibaba.fastjson.JSON;
import com.gitee.easyopen.ApiContext;
import com.gitee.easyopen.ApiMeta;
import com.gitee.easyopen.ApiResult;
import com.gitee.easyopen.interceptor.ApiInterceptorAdapter;
import com.xdjd.mobile.business.util.ResponseUtil;
import com.xdjd.mobile.business.util.SpringContextUtils;
import com.xdjd.mobile.system.config.VersionConfig;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ForceUpdateInterceptor extends ApiInterceptorAdapter {
    Logger logger = LoggerFactory.getLogger(getClass());
    private VersionConfig versionConfig;

    public ForceUpdateInterceptor() {
        versionConfig = SpringContextUtils.getBean(VersionConfig.class);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object serviceObj, Object argu) {
        ApiResult result = new ApiResult();
        String appKey = ApiContext.getApiParam().fatchAppKey();
        String appVersion = ApiContext.getApiParam().getString("appVersion") == null ? "1.0.0" : ApiContext.getApiParam().getString("appVersion");
        if ("android".equals(appKey)) {
            if (appVersion.equals(versionConfig.getAndroidVersion())) {
                return true;
            } else if (StringUtils.isNotBlank(versionConfig.getAndroidForceVersion()) && appVersion.compareTo(versionConfig.getAndroidForceVersion()) < 0) {
                result.setCode("1");
                result.setData(versionConfig.getAndroidDownLoadUrl());
                ResponseUtil.renderJson(response, JSON.toJSONString(result));
                return false;
            } else if (StringUtils.isNotBlank(versionConfig.getAndroidForceVersion()) && appVersion.compareTo(versionConfig.getAndroidForceVersion()) >= 0
                    && appVersion.compareTo(versionConfig.getAndroidVersion()) < 0) {
                return true;
            } else if (StringUtils.isBlank(versionConfig.getAndroidForceVersion()) && appVersion.compareTo(versionConfig.getAndroidVersion()) < 0) {
                return true;
            } else {
                return true;
            }
        } else if ("ios".equals(appKey)) {
            if (appVersion.equals(versionConfig.getIosVersion())) {
                return true;
            } else if (StringUtils.isNotBlank(versionConfig.getIosForceVersion()) && appVersion.compareTo(versionConfig.getIosForceVersion()) <= 0) {
                result.setCode("1");
                ResponseUtil.renderJson(response, JSON.toJSONString(result));
                return false;
            } else if (StringUtils.isNotBlank(versionConfig.getIosForceVersion()) && appVersion.compareTo(versionConfig.getIosForceVersion()) > 0
                    && appVersion.compareTo(versionConfig.getIosVersion()) < 0) {
                return true;
            } else if (StringUtils.isBlank(versionConfig.getIosForceVersion()) && appVersion.compareTo(versionConfig.getIosVersion()) < 0) {
                return true;
            } else {
                return true;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object serviceObj, Object argu,
                           Object result) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object serviceObj, Object argu, Object result, Exception e) throws Exception {
    }

    @Override
    public boolean match(ApiMeta apiMeta) {
        return true;
    }
}
